package com.person.jc.service.base.standard;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.SQLSpell;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdStandardDeviceService extends Service {
	
	public final static BdStandardDeviceService me = new BdStandardDeviceService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select device_id,device_name,measure_scope,memo,cert_code,device_type,device_id_,order_no,date_format(end_date, '%Y-%m-%d') end_date,datediff(end_date , now()) diff_day,"
				+ "("+SQLSpell.kindName("device_type", "t.device_type")+") device_type_name, "
				+ "("+SQLSpell.deviceName("t.device_id_")+") device_id_name "
				+ " from bd_standard_device t where if_valid = 1");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceName")))){
			sql.append(" and device_name like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("certCode")))){
			sql.append(" and cert_code like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceId")))){
			sql.append(" and device_id = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceType")))){
			sql.append(" and device_type = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceIds")))){
			sql.append(" and device_id in ("+ temp +")");

		}
		sql.append(" order by device_type,order_no,diff_day,device_name ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		Record device = new Record();
		device.set("device_name", map.get("deviceName"));
		device.set("measure_scope", map.get("measureScope"));
		device.set("memo", map.get("memo"));
		device.set("cert_code", map.get("certCode"));
		device.set("end_date", map.get("endDate"));
		device.set("device_type", map.get("deviceType"));
		device.set("remark", map.get("remark"));
		
		Object orderNo = Utils.isNotEmpty(String.valueOf(map.get("orderNo")))?map.get("orderNo"):null;
		Object deviceId_ = Utils.isNotEmpty(String.valueOf(map.get("deviceId_")))?map.get("deviceId_"):null;
		device.set("order_no", orderNo);
		device.set("device_id_", deviceId_);
		Db.save("bd_standard_device", device);
		super.setCRUD("计量标准装置", "C", "新增标准装置："+map.get("deviceName"));
	}
	
	public void update(Map<String,Object> map){
		Record device = new Record();
		device.set("device_id", map.get("deviceId"));
		device.set("device_name", map.get("deviceName"));
		device.set("measure_scope", map.get("measureScope"));
		device.set("memo", map.get("memo"));
		device.set("cert_code", map.get("certCode"));
		device.set("end_date", map.get("endDate"));
		device.set("device_type", map.get("deviceType"));
		device.set("remark", map.get("remark"));
		
		Object orderNo = Utils.isNotEmpty(String.valueOf(map.get("orderNo")))?map.get("orderNo"):null;
		Object deviceId_ = Utils.isNotEmpty(String.valueOf(map.get("deviceId_")))?map.get("deviceId_"):null;
		device.set("order_no", orderNo);
		device.set("device_id_", deviceId_);
		Db.update("bd_standard_device", "device_id", device);
		super.setCRUD("计量标准装置", "U", "修改标准装置，编号："+map.get("deviceId"));
	}
	
	public void delete(String deviceId){
		String sql = " update bd_standard_device set if_valid = 0 where device_id = ? ";
		Db.update(sql, deviceId);
		super.setCRUD("计量标准装置", "D", "删除标准装置，编号："+deviceId);
	}
	
	public long checkName(String deviceName, String deviceId){
		String sql = "select count(1) from bd_standard_device where device_name = ? and if_valid <> 0 ";
		if(Utils.isNotEmpty(deviceId)){
			sql += " and device_id <> " + deviceId;
		}
		return Db.queryLong(sql, deviceName);
	}
}
